package com.ck.juc;

import lombok.SneakyThrows;

import java.util.concurrent.CyclicBarrier;

/**
 * 和CountDownLatch的原理相同，CountDownLatch是减法操作
 * CyclicBarrier是加法操作，执行的线程是最后一个线程来执行方法
 */
public class CyclicBarrierDemo {

    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(4, new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("===============");
                System.out.println(Thread.currentThread().getName() + "\t 执行");
            }
        },"aaa"));
        for(int index = 0;index < 4;index++){
            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "\t 执行");
                    cyclicBarrier.await();
                }
            },String.valueOf(index)).start();
        }
    }
}
